/**
 * Title: CommonDialog.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-8-16 下午3:53:45
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.widgets;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luoxudong.phonenumber.R;

/** 
 * ClassName: CommonDialog
 * Description:自定义dialog
 * Create by 罗旭东
 * Date 2013-8-16 下午3:53:45
 */
public class CommonDialog extends Dialog {
	private Context context = null;
	
	public CommonDialog(Context context) 
	{
		super(context);
		this.context = context;
	}
	
	public CommonDialog(Context context, int theme) {
		super(context, theme);
	}
	
	protected CommonDialog(Context context, boolean cancelable, OnCancelListener cancelListener) 
	{
		super(context, cancelable, cancelListener);
	}

	/**
	 * 创建一个定制对话框
	 * 
	 * @param title
	 *            标题
	 * @param message
	 *            内容
	 * @param positiveClickListener
	 *            确认监听器
	 * @param negativeClickListener
	 *            取消监听器
	 * @return dialog
	 * @exception throws [违例类型] [违例说明]
	 * @see
	 */
	public CommonDialog createDialog(String title, String message, OnClickListener positiveClickListener,
			OnClickListener negativeClickListener)
	{
		CommonDialog dialog = null;

		CommonDialog.Builder builder = new CommonDialog.Builder(context);

		// 标题
		if (title != null && !"".equals(title.trim()))
		{
			builder.setTitle(title);
		}

		// 内容
		if (message != null && !"".equals(message.trim()))
		{
			builder.setMessage(message);
		}

		// 确认消息
		if (positiveClickListener != null)
		{
			builder.setPositiveButton(context.getResources().getText(R.string.common_confirm).toString(), positiveClickListener);
		}

		// 取消消息
		if (negativeClickListener != null)
		{
			builder.setNegativeButton(context.getResources().getText(R.string.common_cancel).toString(), negativeClickListener);
		}

		// 创建视图
		dialog = builder.create();
		dialog.show();
		return dialog;
	}

	/**
	 * 创建一个定制对话框
	 * 
	 * @param title
	 *            标题资源id
	 * @param message
	 *            内容资源id
	 * @param positiveClickListener
	 *            确认监听器
	 * @param negativeClickListener
	 *            取消监听器
	 * @return dialog
	 * @exception throws [违例类型] [违例说明]
	 * @see
	 */
	public CommonDialog createDialog(int title, int message, OnClickListener positiveClickListener,
			OnClickListener negativeClickListener)
	{
		CommonDialog dialog = null;

		CommonDialog.Builder builder = new CommonDialog.Builder(context);

		// 标题
		if (context.getResources().getText(title) != null && !"".equals(context.getResources().getText(title).toString().trim()))
		{
			builder.setTitle(context.getResources().getText(title).toString());
		}

		// 内容
		if (context.getResources().getText(message) != null && !"".equals(context.getResources().getText(message).toString()))
		{
			builder.setMessage(context.getResources().getText(message).toString());
		}

		// 确认事件
		if (positiveClickListener != null)
		{
			builder.setPositiveButton(context.getResources().getText(R.string.common_confirm).toString(), positiveClickListener);
		}

		// 取消事件
		if (negativeClickListener != null)
		{
			builder.setNegativeButton(context.getResources().getText(R.string.common_cancel).toString(), negativeClickListener);
		}

		// 创建控件
		dialog = builder.create();
		dialog.show();
		return dialog;
	}

	/**
	 * 弹出框为自定义view
	 * 
	 * @param v
	 * @return dialog
	 * @exception throws [违例类型] [违例说明]
	 * @see
	 */
	public Dialog createDialogWithView(View v)
	{
		CommonDialog dialog = null;
		CommonDialog.Builder builder = new CommonDialog.Builder(context);
		builder.setLayoutView(v);
		// 创建控件
		dialog = builder.create();
		dialog.show();
		return dialog;
	}

	/**
	 * @ClassName: Builder
	 * @Description: Builder类
	 * @author luoxudong
	 * @date 2012-9-11 下午8:13:21
	 */
	public static class Builder
	{
		/**
		 * 上下文
		 */
		private Context context = null;

		/**
		 * 图标资源文件
		 */
		private int icon = 0;

		/**
		 * 标题
		 */
		private String title = null;

		/**
		 * 内容
		 */
		private String message = null;

		/**
		 * 确认按钮字符串
		 */
		private String positiveBtnTxt = null;

		/**
		 * 取消按钮字符串
		 */
		private String negativeBtnTxt = null;

		/**
		 * 内容view
		 */
		private View view = null;

		/**
		 * dialog的自定义view
		 */
		private View layoutView = null;

		/**
		 * 是否按返回键可以取消
		 */
		private boolean cancelable = true;
		
		/**
		 * 对话框view
		 */
		private View layout = null;
		
		/**
		 * 点击区域外面是否可取消
		 */
		private boolean isCanceledOnTouchOutside = true;

		/**
		 * 确认监听器
		 */
		private OnClickListener positiveBtnClickListener = null;

		/**
		 * 取消监听器
		 */
		private OnClickListener negativeBtnClickListener = null;

		/**
		 * 按键消息
		 */
		private OnKeyListener keyListener = null;

		/**
		 * 构造函数
		 * 
		 * @param context
		 */
		public Builder(Context context)
		{
			this.context = context;
		}

		/**
		 * 设置弹出框内容
		 * 
		 * @param message
		 *            弹出框内容
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setMessage(String message)
		{
			this.message = message;
			return this;
		}

		/**
		 * 设置弹出框内容
		 * 
		 * @param message
		 *            内容资源ID
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setMessage(int message)
		{
			this.message = (String) context.getResources().getText(message);
			return this;
		}

		/**
		 * 设置标题图标，根据需求自定义弹出框无标题栏，暂时屏蔽
		 * 
		 * @param icon
		 *            图标资源ID
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setIcon(int icon)
		{
			this.icon = icon;
			return this;
		}

		public Builder setHiddenLine(boolean isLineHidden)
		{
			return this;
		}
		
		/**
		 * 设置弹出框标题，根据需求自定义弹出框无标题栏，暂时屏蔽
		 * 
		 * @param title
		 *            标题
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setTitle(String title)
		{
			this.title = title;
			return this;
		}

		/**
		 * 设置弹出框标题，根据需求自定义弹出框无标题栏，暂时屏蔽
		 * 
		 * @param title
		 *            标题资源ID
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setTitle(int title)
		{
			this.title = (String) context.getResources().getText(title);
			return this;
		}

		/**
		 * 设置当前内容View
		 * 
		 * @param v
		 *            View
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setView(View v)
		{
			this.view = v;
			return this;
		}

		/**
		 * 设置对话框View
		 * 
		 * @param v
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setLayoutView(View v)
		{
			this.layoutView = v;
			return this;
		}

		public Builder setCancelable(boolean cancelable)
		{
			this.cancelable = cancelable;
			return this;
		}
		public Builder setCanceledOnTouchOutside(boolean isCanceledOnTouchOutside)
		{
			this.isCanceledOnTouchOutside = isCanceledOnTouchOutside;
			return this;
		}

		/**
		 * 设置确认按钮信息
		 * 
		 * @param positiveBtnTxt
		 *            确认按钮内容
		 * @param listener
		 *            按钮监听事件
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setPositiveButton(String positiveBtnTxt, OnClickListener listener)
		{
			this.positiveBtnTxt = positiveBtnTxt;
			this.positiveBtnClickListener = listener;
			return this;
		}

		/**
		 * 设置确认按钮信息
		 * 
		 * @param positiveBtnTxt
		 *            确认按钮内容资源ID
		 * @param listener
		 *            确认按钮监听事件
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setPositiveButton(int positiveBtnTxt, OnClickListener listener)
		{
			this.positiveBtnTxt = (String) context.getText(positiveBtnTxt);
			this.positiveBtnClickListener = listener;
			return this;
		}
		
		/**
		 * 设置取消按钮信息
		 * 
		 * @param negativeBtnTxt
		 *            取消按钮内容
		 * @param listener
		 *            取消按钮监听事件
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setNegativeButton(String negativeBtnTxt, OnClickListener listener)
		{
			this.negativeBtnTxt = negativeBtnTxt;
			this.negativeBtnClickListener = listener;
			return this;
		}

		/**
		 * 设置取消按钮信息
		 * 
		 * @param negativeBtnTxt
		 *            取消按钮内容资源ID
		 * @param listener
		 *            取消按钮监听事件
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setNegativeButton(int negativeBtnTxt, OnClickListener listener)
		{
			this.negativeBtnTxt = (String) context.getText(negativeBtnTxt);
			this.negativeBtnClickListener = listener;
			return this;
		}

		/**
		 * 按键消息
		 * 
		 * @param listener
		 *            按键监听器
		 * @return 当前对象
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public Builder setOnKeyListener(OnKeyListener listener)
		{
			this.keyListener = listener;
			return this;
		}

		/**
		 * 修改对话框标题
		 * @param title
		 */
		public void modifyTitle(String title)
		{
			if (title != null)
			{
				((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
			}
		}
		public void modifyTitle(int title)
		{
			if (title != 0)
			{
				((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
			}
		}
		
		/**
		 * 创建视图
		 * 
		 * @return CommonDialog
		 * @exception throws [违例类型] [违例说明]
		 * @see
		 */
		public CommonDialog create()
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final CommonDialog dialog = new CommonDialog(context, R.style.CommonDialog);
			layout = inflater.inflate(R.layout.common_dialog, null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

			if (icon != 0)
			{
				((ImageView) layout.findViewById(R.id.dialog_icon)).setBackgroundResource(icon);
			}

			if (title != null)
			{
				((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
			}
			else
			{
				((LinearLayout) layout.findViewById(R.id.ll_title)).setVisibility(View.GONE);
			}

			// 设置消息
			if (message != null)
			{
				((TextView) layout.findViewById(R.id.dialog_message)).setText(message);
			}

			if (message != null && view != null)//存在内容字符串以及控件时
			{
				LinearLayout contentLayout = (LinearLayout) layout.findViewById(R.id.layout_content);
				contentLayout.removeAllViews();
				contentLayout.addView(view, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			}
			else if (view != null)
			{
				LinearLayout contentLayout = (LinearLayout) layout.findViewById(R.id.layout_content);
				contentLayout.removeAllViews();
				contentLayout.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			}

			// 设置确定按钮
			if (positiveBtnTxt != null && !"".equals(positiveBtnTxt))
			{
				LinearLayout positiveBtn = (LinearLayout) layout.findViewById(R.id.dialog_button_ok);
				TextView positiveTextView = (TextView)layout.findViewById(R.id.tv_dialog_button_ok);
				positiveTextView.setText(positiveBtnTxt);

				positiveBtn.setOnClickListener(new View.OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						if (positiveBtnClickListener != null)
						{
							positiveBtnClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
						}

						dialog.dismiss();
					}
				});
			}
			else
			{
				layout.findViewById(R.id.dialog_button_ok).setVisibility(View.GONE);
			}

			// 设置取消按钮
			if (negativeBtnTxt != null && !"".equals(negativeBtnTxt))
			{
				LinearLayout negativeBtn = (LinearLayout) layout.findViewById(R.id.dialog_button_cancel);
				TextView negativeTextView = (TextView)layout.findViewById(R.id.tv_dialog_button_cancel);
				negativeTextView.setText(negativeBtnTxt);

				negativeBtn.setOnClickListener(new View.OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						if (negativeBtnClickListener != null)
						{
							negativeBtnClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
						}
						dialog.dismiss();
					}
				});
			}
			else
			{
				layout.findViewById(R.id.dialog_button_cancel).setVisibility(View.GONE);
			}

			// 没有按钮是去掉按钮栏
			if ((positiveBtnTxt == null || "".equals(positiveBtnTxt)) && (negativeBtnTxt == null || "".equals(negativeBtnTxt)))
			{
				layout.findViewById(R.id.ll_dialog_btn).setVisibility(View.GONE);
			}

			// 按键消息
			if (keyListener != null)
			{
				dialog.setOnKeyListener(keyListener);
			}

			if (layoutView != null)// 设置自定义view
			{
				LinearLayout mainLayout = (LinearLayout) layout.findViewById(R.id.dialog_layout);
				mainLayout.removeAllViews();
				mainLayout.setBackgroundColor(Color.TRANSPARENT);// 去掉背景
				mainLayout.setMinimumHeight(0);
				mainLayout.setMinimumWidth(0);
				mainLayout.addView(layoutView);
				dialog.setCanceledOnTouchOutside(true);
			}

			dialog.setCancelable(cancelable);
			dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside);

			dialog.setContentView(layout);
			return dialog;
		}
	}
}
